﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using DMS.BaseClass;
using DMS.Business.Modules.ArchivesManagement;
using DMS.Data;
using DMS.Business.Utility;

namespace DMS.Web.Modules.ArchivesManagement
{
    public partial class BaseInfoImportConfirm : System.Web.UI.Page
    {
        private string _id = "";
        private ActionType _action;


        protected void Page_Load(object sender, EventArgs e)
        {
            //_id = Request["id"];
            //_action = (ActionType)Enum.Parse(typeof(ActionType), Request["action"].ToString());

            if (!IsPostBack)
            {
                //InitControls();
                Search();
                //InitData();
            }
        }

        private void InitControls()
        {
           
        }

        private void InitData()
        {
            
        }

        private void Search()
        {
            int total;
            string batchId = Request.QueryString["batchId"];
            string condition = string.Format(" and it.batchId='{0}'",batchId);//GetCondition();
            //缓存到页面隐藏字段
            //this.Condition.Value = condition;

            if (this.PageSize.Text == "")
                this.PageSize.Text = "15";
            pager.PageSize = int.Parse(this.PageSize.Text);
            List<AM_BaseInfoTemp> list = BaseInfoTempService.GetBaseInfoTemp(condition, pager.CurrentPageIndex,
                                                                             pager.PageSize, out total);

            //foreach (GridViewRow gr in baseInfoList.Rows)
            //{
            //    HtmlInputCheckBox cb = (HtmlInputCheckBox) gr.Cells[0].FindControl("chkItem");
            //    string id = cb.Attributes["value"].Split(',')[0];
            //    if (ViewState[id] != null)
            //    {
            //        ViewState.Remove(id);
            //    }
            //}

            // 获取GridView排序数据列及排序方向
            string sortExpression = this.baseInfoList.Attributes["SortExpression"];
            string sortDirection = this.baseInfoList.Attributes["SortDirection"];

            IQueryable<AM_BaseInfoTemp> queryableList;
            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                //调用排序方法对当前页数据进行排序
                queryableList = (list.AsQueryable()).OrderUsingSortExpression(sortExpression + " " + sortDirection);
            }
            else
            {
                queryableList = list.AsQueryable();
            }

            BaseInfos = queryableList;

            // GridView绑定并显示数据
            this.baseInfoList.DataSource = queryableList;
            this.baseInfoList.DataBind();

            pager.RecordCount = total;
            TotalCount.Text = total.ToString();
            ErrorCount.Text = BaseInfoTempService.GetCount("Error",batchId).ToString();
            WarningCount.Text = BaseInfoTempService.GetCount("Warning",batchId).ToString();
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "scriptKey", "initControls();", true);
        }

        private IQueryable<AM_BaseInfoTemp> BaseInfos { get; set; }

        protected void baseInfoList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                AM_BaseInfoTemp baseInfo = BaseInfos.ToList()[e.Row.RowIndex];

                TextBox InsuranceCode = e.Row.FindControl("InsuranceCode") as TextBox;
                TextBox UnitName = e.Row.FindControl("UnitName") as TextBox;
                TextBox TaxCode = e.Row.FindControl("TaxCode") as TextBox;
                InsuranceCode.Text = baseInfo.InsuranceCode;
                UnitName.Text = baseInfo.UnitName;
                TaxCode.Text = baseInfo.TaxCode;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Search();
        }

        protected void DealCommand(object sender, GridViewCommandEventArgs e)
        {
            string commandName = e.CommandName;
            var id = e.CommandArgument as string;
            if (commandName.Equals("DeleteBaseInfo"))
            {
                BaseInfoTempService.RemoveBaseInfoTemp(id);
            }
            Search();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void pager_PageChanged(object sender, EventArgs e)
        {
            Search();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string batchId = Request.QueryString["batchId"];
                BaseInfoTempService.CopyTempToBaseInfo(batchId);
                ClientHelper.Instance.CloseWindow("保存成功", "ok");
            }
            catch (Exception ex)
            {
                string code = LogHelper.LogException(ex);
                string msg = string.Format("操作失败，请联系管理员。\\r\\n错误码：{0}", code);
                ClientHelper.Instance.Alert(msg);
            }
        }

        protected void ProvinceID_SelectedIndexChanged(object sender, EventArgs e)
        {
            //List<SYS_Address_City> list = AddressService.GetCitysByProvinceId(ProvinceID.SelectedValue);
            //CityID.DataSource = list;
            //CityID.DataBind();

            //foreach (ListItem item in CityID.Items)
            //{
            //    if (item.Text == "广州市")
            //    {
            //        item.Selected = true;
            //        CityID_SelectedIndexChanged(null, null);
            //        break;
            //    }
            //}
        }

        protected void CityID_SelectedIndexChanged(object sender, EventArgs e)
        {
            //List<SYS_Address_District> list = AddressService.GetDistrictsByCityId(CityID.SelectedValue);
            //DistrictID.DataSource = list;
            //DistrictID.DataBind();
        }
    }
}